<template>
  <el-container class="home_container">
    <el-header>
      <div><img src="../assets/logo1.png" alt=""><span>电商系统</span></div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <el-menu
          background-color="#303440"
          text-color="#fff"
          active-text-color="#6094C7" :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-submenu :index="String(item.id)" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavstate(subItem.path)">
              <i class="el-icon-menu"></i><span>{{ subItem.authName }}</span></el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user-group-fill',
        103: 'iconfont icon-databaseset-fill',
        101: 'iconfont icon-box-fill',
        102: 'iconfont icon-calendaralt-fill',
        145: 'iconfont icon-chart-line'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = sessionStorage.getItem('active_path')
  },
  methods: {
    logout () {
      sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return console.log(res.meta.msg)
      this.menulist = res.data
      // console.log(res.data)
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavstate (activePath) {
      sessionStorage.setItem('active_path', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;

  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #303440;
    color: #fff;
    font-size: 20px;

    > div {
      display: flex;
      align-items: center;

      > span {
        margin-left: 10px;
      }
    }
  }

  .el-aside {
    background-color: #303440;

    .el-menu {
      border-right: none;

      .toggle-button {
        height: 24px;
        background-color: #47505D;
        font-size: 10px;
        color: #909399;
        line-height: 24px;
        text-align: center;
        cursor: pointer;
        letter-spacing: 0.2em;
      }
    }
  }
}

.iconfont {
  margin-right: 5px;
}
</style>
